﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.DBUtility;
using MeTalker.Aide.Model;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal partial class frmAutoReply : MeForm
    {
        int pageSize = 10;
        int currIndex;
        int MaxPage;

        List<AnswerMod> answers;
        public frmAutoReply()
        {
            InitializeComponent();
            this.dataGridView.AutoGenerateColumns = false;
            this.dataGridView.CellContentClick += new DataGridViewCellEventHandler(dataGridView_CellContentClick);
            answers = AccessHelper.GetProblemList();
            MaxPage = answers.Count;
            BindData(0);
            txtPageIndex.Text = MaxPage.ToString();
        }

        void BindData(int pageIndex)
        {
            currIndex = pageIndex;
            dataGridView.DataSource = null;
            dataGridView.DataSource = answers.Skip((pageSize * pageIndex)).Take(pageSize).ToList(); ;
        }

        void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1) //单元格当然要排除列头
            {
                // DataGridViewCell cell = this.dataGridView[0, e.RowIndex];
                AnswerMod answer = this.dataGridView.CurrentRow.DataBoundItem as AnswerMod;
                //也要判断当前点击单元格的名称是否是Link列的名称
                if (this.dataGridView.Columns[e.ColumnIndex].Name == "edit" && answer != null)
                {
                    List<AnswerMod> lists = dataGridView.DataSource as List<AnswerMod>;
                    if (lists != null)
                    {
                        DialogResult result = new frmAutoReplyEdit(answer).ShowDialog();
                        if (result == DialogResult.Yes)
                        {
                            BindData(currIndex);
                        }
                    }
                }
                else if (this.dataGridView.Columns[e.ColumnIndex].Name == "delete" && answer != null)
                {
                    AccessHelper.DeleteProblem(answer.Id);
                    BindData(currIndex);
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAutoReplyEdit edit = new frmAutoReplyEdit();
            DialogResult result = edit.ShowDialog();
            if (result == DialogResult.Yes)
            {
                answers.Add(edit.Answer);
                BindData(currIndex);
            }
        }

        #region dataGridView_CellClick
        private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                AnswerMod answer = this.dataGridView.SelectedRows[0].DataBoundItem as AnswerMod;
                if (answer == null)
                    return;
                if (this.dataGridView.Columns[e.ColumnIndex].Name == "edit" && answer != null)
                {
                    frmAutoReplyEdit edit = new frmAutoReplyEdit(answer);
                    DialogResult result = edit.ShowDialog();
                    if (result == DialogResult.Yes)
                    {
                        this.dataGridView.Refresh();
                    }
                }
                else if (this.dataGridView.Columns[e.ColumnIndex].Name == "delete" && answer != null)
                {
                    int effect = AccessHelper.DeleteProblem(answer.Id);
                    if (effect > 0)
                    {
                        dataGridView.Rows.Remove(dataGridView.CurrentRow);
                        //dataGridView.Rows.Remove(this.dataGridView.Rows[e.RowIndex]);
                        BindData(currIndex);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        #endregion

        private void btnDeleteAll_Click(object sender, EventArgs e)
        {
            List<AnswerMod> answers = dataGridView.DataSource as List<AnswerMod>;
            if (answers != null)
            {
                foreach (var answer in answers)
                {
                    AccessHelper.DeleteProblem(answer.Id);
                }
                BindData(currIndex);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "*.jlso|*.jlso";
            save.FileName = "";
            save.ShowDialog();
            string fileName = save.FileName;
            if (!string.IsNullOrEmpty(fileName))
            {
                List<AnswerMod> tmps = new List<AnswerMod>();
                foreach (var ds in answers)
                {
                    tmps.Add(new AnswerMod() { Id = ds.Id, Answer = ds.Answer, Problem = ds.Problem.Replace("  ", "|") });
                }
                SerializerFactory.SerializeFile<List<AnswerMod>>(tmps, fileName);
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "*.jlso|*.jlso";
            open.FileName = "";
            open.ShowDialog();
            string fileName = open.FileName;
            if (!string.IsNullOrEmpty(fileName))
            {
                List<AnswerMod> tmp = SerializerFactory.DeserializeFile<List<AnswerMod>>(fileName) as List<AnswerMod>;
                foreach (var ds in tmp)
                {
                    AnswerMod tmps = new AnswerMod() { Id = ds.Id, Answer = ds.Answer, Problem = ds.Problem.Remove(0, 3).Replace("问题:", "\r\n").Replace("|", "") };
                    AccessHelper.InsertProblem(tmps);
                }
                answers = AccessHelper.GetProblemList();
                MaxPage = answers.Count;
                BindData(0);
            }
        }

        private void btnNextPage_Click(object sender, EventArgs e)
        {
            if (MaxPage > currIndex)
                BindData(currIndex + 1);
        }

        private void btnPreviousPage_Click(object sender, EventArgs e)
        {
            if (currIndex > 0)
                BindData(currIndex - 1);
        }

        private void btnPageIndex_Click(object sender, EventArgs e)
        {
            int index;
            int.TryParse(txtPageIndex.Text, out index);
            if (MaxPage <= index && index > 0)
                BindData(index - 1);
        }
    }
}